package wwk.common.widget;

import java.util.ArrayList;

import wwk.common.util.LayoutUtil;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NavigationBar extends RelativeLayout {
	
	static final int TitleViewID=1;
	static final int RightButtonBaseId=10000;
	static final int ButtonSpace=20;
	public Activity activity;
	
	private ArrayList<Integer> rightButtonIdArray=null;

	public NavigationBar(Context context) 
	{
		super(context);

		init();
	}
	
	public NavigationBar(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		init();
	}
	
	public static NavigationBar getDefaultNavigationBar(Context context)
	{
		 NavigationBar nav=new NavigationBar(context);
		 nav.setBackgroundColor(0xff00A0EE);
		 return nav;
	}
	
	private void init()
	{
		//title
		TextView textView=new TextView(this.getContext());
		textView.setText("");
		textView.setTextSize(20);
		textView.setTextColor(Color.WHITE);
		textView.setId(TitleViewID);

		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_IN_PARENT);
		int margin=LayoutUtil.dip2px(getContext(), 90);
		lp.setMargins(margin,0,margin,0);
		textView.setLayoutParams(lp);
		//textView.setMaxWidth(200);
		textView.setSingleLine();
		textView.setEllipsize(TruncateAt.END);

		addView(textView);
		
		/*
		RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		this.setLayoutParams(layoutParams);
		this.setBackgroundColor(0x00a0ee);
		*/
	}
	
	public void setTitle(String title)
	{
		TextView titleView =(TextView) this.findViewById(TitleViewID);
		if(titleView!=null)
		{
			titleView.setText(title);
		}
	}
	
	public void setTitleView(View titleView)
	{
		
	}
	
	public View setBackButton(int layoutId)
	{
	 	View button=(View)LayoutUtil.getInflaterLayout(this.getContext(), layoutId);
	 	addView(button);
	 	
	 	RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)button.getLayoutParams();
	 	lp.setMargins(10,0,0,0);
	 	
	 	button.setOnClickListener(new OnClickListener()
	 	{
			@Override
			public void onClick(View arg0) 
			{
				activity.finish();
			}
	 	});
	 	
	 	return button;
	}
	
	public View addRightButton(int layoutId)
	{		
	 	View button=LayoutUtil.getInflaterLayout(this.getContext(), layoutId);

	 	return addRightView(button);
	}
	
	public View addRightButtonDrawable(int drawableId)
	{		
	 	ImageButton button=new ImageButton(getContext());
	 	button.setLayoutParams(LayoutUtil.getLayoutParamsWrapContent());
	 	button.setBackgroundColor(Color.TRANSPARENT);
	 	button.setImageResource(drawableId);

	 	return addRightView(button);
	}
	
	public View addRightView(View button)
	{		
	 	addView(button);

	 	RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)button.getLayoutParams();
	 	lp.setMargins(0,0,ButtonSpace,0);
	 	lp.addRule(RelativeLayout.CENTER_VERTICAL);
	 	
	 	if(rightButtonIdArray==null)
		{
			rightButtonIdArray=new ArrayList<Integer>();
		}
	 	
		int rightButtonCount=rightButtonIdArray.size();
	 	
	 	if(rightButtonCount==0)
	 	{
	 		lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
	 	}
	 	else
	 	{
	 		int id=rightButtonIdArray.get(rightButtonCount-1);
	 		lp.addRule(RelativeLayout.LEFT_OF,id);
	 	}
	 	
	 	int id=RightButtonBaseId+rightButtonCount;
	 	button.setId(id);
 		rightButtonIdArray.add(id);
	 	
	 	return button;
	}
}
